//
//  LLKPosMatcher.m
//  Lianliankan
//
//  Created by 李佳 on 15/3/15.
//  Copyright (c) 2015年 LiJia. All rights reserved.
//

#import "PosMatcher.h"

@implementation LLKPosMatcher

//同一个线上

/*
 a------b;
 */

- (BOOL)horizonLine: (CGPoint)firstPos and: (CGPoint)sceondPos
{
    //
    if (firstPos.x == sceondPos.x && firstPos.y == sceondPos.y)
        return false;
    if (firstPos.y != sceondPos.y)
        return false;
    NSUInteger start = MIN(firstPos.x, sceondPos.x);
    NSUInteger end = MAX(firstPos.x, sceondPos.x);
    for (NSUInteger clo = start + 1; clo < end; ++clo) //行加1
    {
        if ([self.LLKMapArray[(int)firstPos.y][clo] intValue] != 0)
            return false;
    }
    
    return true;
}
/*
 a
 |
 |
 |
 b
 */
- (BOOL)vertical: (CGPoint)firstPos and: (CGPoint)sceondPos
{
    if (firstPos.x == sceondPos.x && firstPos.y == sceondPos.y)
        return false;
    if (firstPos.x != sceondPos.x)
        return false;
    NSUInteger start = MIN(firstPos.y, sceondPos.y);
    NSUInteger end = MAX(firstPos.y, sceondPos.y);
    for (NSUInteger clo = start + 1; clo < end; ++clo) //列加1
    {
        if ([self.LLKMapArray[clo][(int)firstPos.x] intValue] != 0)
            return false;
    }
    
    return true;
}

//一个拐角（对角线）
/*******************

 a------c
 |******|
 |******|
 d------b
 
 *******************/

- (NSArray*)oneCorner: (CGPoint)firstPos and: (CGPoint)sceondPos
{
    CGPoint cornerPoint1 = CGPointMake(firstPos.x, sceondPos.y);
    CGPoint cornerPoint2 = CGPointMake(sceondPos.x, firstPos.y);

    if ([self.LLKMapArray[(int)cornerPoint1.y][(int)cornerPoint1.x] intValue] == 0)
    {
        if([self vertical:firstPos and:cornerPoint1] && [self horizonLine:cornerPoint1 and:sceondPos])
            return @[[NSValue valueWithCGPoint:firstPos], [NSValue valueWithCGPoint:cornerPoint1],
                     [NSValue valueWithCGPoint:sceondPos]];
    }
    
    if ([self.LLKMapArray[(int)cornerPoint2.y][(int)cornerPoint2.x] intValue] == 0)
    {
        if([self horizonLine:firstPos and:cornerPoint2] && [self vertical:cornerPoint2 and:sceondPos])
            return @[[NSValue valueWithCGPoint:firstPos], [NSValue valueWithCGPoint:cornerPoint2],
                     [NSValue valueWithCGPoint:sceondPos]];
    }
    
    return nil;
}

//两个拐角

//一个拐角（对角线）
/*******************
 
 a-------
 *******|
 *******|
 ****b---
 
 *******************/
- (NSArray*)twoCorner: (CGPoint)firstPos and: (CGPoint)secondPos
{
    //下面这几个 还有一个优化的空间。
    
    
    //检测a点,b点的左侧是否能够垂直直连
    for (int i = firstPos.x - 1; i >= 0; --i)
    {
        CGPoint firstLeft = CGPointMake(i, firstPos.y);
        CGPoint secondLeft = CGPointMake(i, secondPos.y);
        if ([self.LLKMapArray[(int)firstLeft.y][(int)firstLeft.x] intValue] == 0
            &&[self.LLKMapArray[(int)firstLeft.y][(int)firstLeft.x] intValue] == 0)
        {
            if ([self vertical:firstLeft and:secondLeft] &&
                [self horizonLine:firstPos and:firstLeft] &&
                [self horizonLine:secondLeft and:secondPos])
            {
                return @[[NSValue valueWithCGPoint:firstPos],
                         [NSValue valueWithCGPoint:firstLeft],
                         [NSValue valueWithCGPoint:secondLeft],
                         [NSValue valueWithCGPoint:secondPos]];
            }
        }

    }
    //检测a点,b点的右侧是否能够垂直直连
    for (int i = firstPos.x + 1; i < ((NSArray*)self.LLKMapArray[0]).count; ++i)
    {
        CGPoint firstRight = CGPointMake(i, firstPos.y);
        CGPoint secondRight= CGPointMake(i, secondPos.y);
        if ([self.LLKMapArray[(int)firstRight.y][(int)firstRight.x] intValue] == 0
            &&[self.LLKMapArray[(int)secondRight.y][(int)secondRight.x] intValue] == 0)
        {
            if ([self vertical:firstRight and:secondRight] &&
                [self horizonLine:firstPos and:firstRight] &&
                [self horizonLine:secondRight and:secondPos])
            {
                return @[[NSValue valueWithCGPoint:firstPos],
                         [NSValue valueWithCGPoint:firstRight],
                         [NSValue valueWithCGPoint:secondRight],
                         [NSValue valueWithCGPoint:secondPos]];
            }
        }
    }
    //检测a点,b点的上侧是否能够水平直连
    for (int i = firstPos.y - 1; i >= 0; --i)
    {
        CGPoint firstTop = CGPointMake(firstPos.x, i);
        CGPoint secondTop = CGPointMake(secondPos.x, i);
        if ([self.LLKMapArray[(int)firstTop.y][(int)firstTop.x] intValue] == 0
            &&[self.LLKMapArray[(int)secondTop.y][(int)secondTop.x] intValue] == 0)
        {
            if ([self horizonLine:firstTop and:secondTop] &&
                [self vertical:firstPos and:firstTop] &&
                [self vertical:secondTop and:secondPos])
            {
                return @[[NSValue valueWithCGPoint:firstPos],
                         [NSValue valueWithCGPoint:firstTop],
                         [NSValue valueWithCGPoint:secondTop],
                         [NSValue valueWithCGPoint:secondPos]];
            }
        }
    }
    
    //检测a点,b点的下侧是否能够水平直连
    for (int i = firstPos.y + 1; i < self.LLKMapArray.count; ++i)
    {
        CGPoint firstBtm = CGPointMake(firstPos.x, i);
        CGPoint secondBtm = CGPointMake(secondPos.x, i);
        if ([self.LLKMapArray[(int)firstBtm.y][(int)firstBtm.x] intValue] == 0
            &&[self.LLKMapArray[(int)secondBtm.y][(int)secondBtm.x] intValue] == 0)
        {
            if ([self horizonLine:firstBtm and:secondBtm] &&
                [self vertical:firstPos and:firstBtm] &&
                [self vertical:secondBtm and:secondPos])
            {
                return @[[NSValue valueWithCGPoint:firstPos],
                         [NSValue valueWithCGPoint:firstBtm],
                         [NSValue valueWithCGPoint:secondBtm],
                         [NSValue valueWithCGPoint:secondPos]];
            }
        }
    }
    return nil;
}

- (NSArray*)LLKBtnMatchWithPoint: (CGPoint)firstPos and: (CGPoint)sceondPos
{
    if (firstPos.x == sceondPos.x && firstPos.y == sceondPos.y)
    {
        //NSLog(@"同一个点！！！");
        return false;
    }
    if ([self.LLKMapArray[(int)firstPos.y][(int)firstPos.x] intValue] != [self.LLKMapArray[(int)sceondPos.y][(int)sceondPos.x] intValue])
    {
        //NSLog(@"值不同");
        return false;
    }
    if ([self horizonLine:firstPos and:sceondPos] ||
        [self vertical:firstPos and:sceondPos])
    {
        return @[[NSValue valueWithCGPoint:firstPos], [NSValue valueWithCGPoint:sceondPos]];
    }
    
    NSArray* oneCornerResult = [self oneCorner:firstPos and:sceondPos];
    if (oneCornerResult)
        return oneCornerResult;
    
    NSArray* twoCornerResult = [self twoCorner:firstPos and:sceondPos];
    if (twoCornerResult)
        return twoCornerResult;
    return nil;
}
@end
